package com.timmy.google_play.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.timmy.google_play.R;

/**
 * Created by Timmy on 2016/12/12.
 * 已知宽度,动态计算高度
 */

public class RatioFrameLayout extends FrameLayout {
    public static final int RELATIVE_WIDTH  = 0;
    public static final int RELATIVE_HEIGHT = 1;
    float mPicRatio = 2.43f;
    int   relative  = RELATIVE_WIDTH;

    /**
     * 设置图片的宽高比
     */
    public void setPicRatio(float picRatio) {
        mPicRatio = picRatio;
    }

    /**
     * 设置计算的相对值
     */
    public void setRelative(int relative) {
        this.relative = relative;
    }

    public RatioFrameLayout(Context context) {
        this(context, null);
    }

    public RatioFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        //取出attrs 中自定义的属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioFrameLayout);

        mPicRatio = typedArray.getFloat(R.styleable.RatioFrameLayout_picRatio, 1);

        relative = typedArray.getInt(R.styleable.RatioFrameLayout_relative, RELATIVE_WIDTH);

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     //动态计算高度

        /*
         AT_MOST: 用的比较少了
         EXACTLY :　match_content,100dp,100px 确切的
         UNSPECIFIED :　warp_content 不确切的
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY && relative == RELATIVE_WIDTH) {     //如果mode是确切的
            //得到自身的宽度
            int selfWidth = MeasureSpec.getSize(widthMeasureSpec);

            //计算应有的高度   图片的宽高比 = 自身的width/自身的heigth
            int selfHeight = (int) (selfWidth / mPicRatio + 0.5f);

            //保存测绘结果
            setMeasuredDimension(selfWidth, selfHeight);

            //作为容器,有义务要去测绘孩子,任何容器都是

            int childWidth  = selfWidth - getPaddingRight() - getPaddingLeft();
            int childheight = selfHeight - getPaddingBottom() - getPaddingTop();

            int ChildWidthMeasureSpec  = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
            int ChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childheight, MeasureSpec.EXACTLY);

            measureChildren(ChildWidthMeasureSpec, ChildHeightMeasureSpec);
        } else if (heightMode == MeasureSpec.EXACTLY && relative == RELATIVE_HEIGHT) {

            //得到自身的高度
            int selfHeight = MeasureSpec.getSize(heightMeasureSpec);

            //计算应有的宽度   图片的宽高比 = 自身的width/自身的heigth
            int selfWidth = (int) (selfHeight * mPicRatio + 0.5f);

            //保存测绘结果
            setMeasuredDimension(selfWidth, selfHeight);

            //作为容器,有义务要去测绘孩子,任何容器都是

            int childWidth  = selfWidth - getPaddingRight() - getPaddingLeft();
            int childheight = selfHeight - getPaddingBottom() - getPaddingTop();

            int ChildWidthMeasureSpec  = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
            int ChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childheight, MeasureSpec.EXACTLY);

            measureChildren(ChildWidthMeasureSpec, ChildHeightMeasureSpec);

        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}
